<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>

    <script src="./libs/jquery.twbsPagination.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center">
                <ul id="pagination" class="pagination-sm"></ul>
            </div>
        </div>
    </div>
</body>
<script src="./libs/https.js"></script>
<script type="text/html" id="tpl-category">
    <!-- 页面中的所有分类不能放入each循环中,否则每生成一个,就产生一个所有分类 -->
<option value="">所有分类</option>
{{each}}
<!-- 循环过程中,为option添加value属性, -->
<option value="{{$value.id}}">{{$value.name}}</option>

{{/each}}
</script>
<script type="text/html" id="tpl-dataArr">
    {{each}}
    <tr>
        <td>[{{$value.title}}]</td>
        <td>{{$value.author}}</td>
        <td>{{$value.category}}</td>
        <td class="text-center">{{$value.date}}</td>
        <td class="text-center">{{$value.state}}</td>
        <td class="text-center">
            <!--在地址中添加id -->
            <a href="article_edit.html?article={{$value.id}}" dataId="{{$value.id}}" class="btn btn-default btn-xs">编辑</a>
          <a href="javascript:void(0);" dataId="{{$value.id}}" class="btn btn-danger btn-xs delete">删除</a>
        </td>
    </tr>
    {{/each}}

</script>
<script>
    $(function () {
        // 设置一个新的函数，默认值为1
        let userpage=1
        // 发起ajax请求
        $.ajax({
            type: 'get',
            url: BigNew.category_list,
            success: function (backData) {
                // console.log(backData);
                let dataArr = backData.data
                // 使用模板引擎将数据渲染到select下拉列表中
                let category = template('tpl-category', dataArr)
                // console.log(category);
                $('#selCategory').html(category)
            }
        })


        function locaData(page) {
            $.ajax({
                type: 'get',
                url: BigNew.article_query,
                data: {
                    type: $('#selCategory').val(),
                    state: $('#selStatus').val(),
                    page: page,
                    perpage: '4'

                },
                success: function (backData) {
                    // console.log(backData);
                    if (backData.data.totalPage > 0) {
                        if (backData.code == 200) {
                            let dataArr = backData.data.data
                            let dataStr = template('tpl-dataArr', dataArr)
                            $('tbody').html(dataStr)
                        }
                        $('#pagination').twbsPagination('destroy');
                        $('#pagination').twbsPagination({
                            // 获取最大页数
                            totalPages: backData.data.totalPage,
                            // 当前可见页数
                            visiblePages: 7,
                            // 关闭初始化点击事件
                            initiateStartPageClick: false,

                            startPage: page,

                            first: '首页',
                            last: '尾页',
                            next: '下一页',
                            prev: '上一页',
                            // 回调函数，有两个返回值：（page）当前的页码[回调函数作用：与后台数据与产生联动]
                            onPageClick: function (e, page) {
                                // console.log(page);
                                userpage = page
                                locaData(page)

                            }
                        });
                    } else {
                        $('tbody').empty();
                        $('#pagination').html('当前分类没有任何数据,敬请期待...');
                    }

                }
            })
        }
        locaData(1)

        // 文章筛选功能
        $('#btnSearch').on('click', function (e) {
            e.preventDefault()
            locaData(1)
        })

        // 删除功能
        $('tbody').on('click','.delete',function(){
            let dataId = $(this).attr('dataid')
            console.log(dataId);
            $.ajax({
                type: 'post',
                url:BigNew.article_delete,
                data: {
                    id:dataId
                },
                success: function(backData) {
                    console.log(backData);
                    if($('tbody tr').length>1||userpage==1){
                        locaData(userpage)
                    }else{
                        locaData(--userpage)
                    }
                }
            })
        })
    })
</script>

</html>